﻿Imports System.Windows.Controls
Imports System.Windows
Imports System.Windows.Media.Imaging
Imports System.Collections.Generic
Imports System.IO
Imports System.Reflection

Public Class Metrolizer
    Private wrapPanelX As Double = 0

    Public Sub DisplayTiles(ByRef metroStackPanel As StackPanel, ByVal ModulesList As List(Of ModulePermissions), ByVal IsSuperUser As Boolean)
        'Dim alphabet() As String = {"a", "b", "c", "d", "e", "f", "g", "h", "i", _
        '                            "j", "k", "l", "m", "n", "o", "p", "q", "r", _
        '                            "s", "t", "u", "v", "w", "x", "y", "z"}

        'Dim di As Dictionary(Of String, String()) = New IconsAndPaths().GetIconsAndPaths()

        'For Each s As String In alphabet
        '    Dim letter As String = s
        '    Dim coll = di.Where(Function(k) k.Key.StartsWith(letter, True, Nothing))
        '    If (coll.Count > 0) Then
        '        AddTiles(coll, metroStackPanel, letter)
        '    End If
        'Next

        Dim mi As List(Of ModuleInfo)
        If IsSuperUser = False Then
            mi = New IconsAndPaths().GetIconsAndPaths(ModulesList)
        Else
            mi = New IconsAndPaths().GetIconsAndPaths
        End If

        For Each m In mi
            AddTiles(m, metroStackPanel)
        Next

        metroStackPanel.Children.Add(tileWrapPanel)
    End Sub

    Dim tileWrapPanel As WrapPanel

    Private Sub AddTiles(ByVal mi As ModuleInfo, ByRef metroStackPanel As StackPanel)
        'For Each kvp As KeyValuePair(Of String, String()) In coll
        Dim newTile As New Tile
        'newTile.ExecutablePath = kvp.Value(1)
        If String.IsNullOrEmpty(mi.tileIconPath) = False Then
            Dim img As New BitmapImage
            Dim buffer As Byte() = System.IO.File.ReadAllBytes(mi.tileIconPath)
            Dim ms = New MemoryStream(buffer)
            img.BeginInit()
            img.StreamSource = ms
            img.EndInit()
            img.Freeze()
            newTile.TileIcon.Source = img
        End If
        newTile.TileTxtBlck.Text = mi.ModuleTitle
        newTile.Margin = New Thickness(0, 0, 6, 6)
        tileWrapPanel.Children.Add(newTile)
        'Next
    End Sub

    ''' <summary>
    ''' Determines the probable location of a WrapPanel that is added
    ''' to MetroStackPanel (assuming that MetroStackPanel was
    ''' like a Canvas).
    ''' </summary>
    ''' <param name="letter">The alphabetical letter representing a WrapPanel group
    ''' in MetroStackPanel.</param>
    ''' <param name="tileWrapPanel">The WrapPanel that was added to MetroStackPanel.</param>
    ''' <remarks></remarks>
    Private Sub WrapPanelLocation(ByVal letter As String, ByVal tileWrapPanel As WrapPanel)
        If (WrapPanelDi.Count = 0) Then
            WrapPanelDi.Add(letter, 0)
        Else
            WrapPanelDi.Add(letter, wrapPanelX)
        End If

        ' Increase value of wrapPanelX as appropriate. 
        ' 6 is right margin of a Tile. 
        If (tileWrapPanel.Children.Count <= 2) Then
            wrapPanelX += ((110 + 6) + 18)
        Else
            Dim numberOfColumns As Double = Math.Ceiling(tileWrapPanel.Children.Count / 2)
            Dim x As Double = (numberOfColumns * 110) + (numberOfColumns * 6) + 18
            wrapPanelX += x
        End If
    End Sub

    Public Sub New()
        tileWrapPanel = New WrapPanel
        tileWrapPanel.Orientation = Orientation.Vertical
        tileWrapPanel.Margin = New Thickness(0, 0, 20, 0)
        ' 3 tiles height-wise
        tileWrapPanel.Height = (110 * 2) + (6 * 2)
    End Sub
End Class
